<script setup>
import { getMenuList } from '@/api/user'
import { useUserStore } from '@/stores'
import { ref } from 'vue'
import { useRouter } from 'vue-router'

const userStore = useUserStore()
const router = useRouter()
// 左侧菜单列表
const menuList = ref([])
const iconsObj = ref({
  125: 'iconfont icon-yonghu',
  103: 'iconfont icon-tijikongjian',
  101: 'iconfont icon-shangpin',
  102: 'iconfont icon-danju',
  145: 'iconfont icon-baobiao',
})
// 被激活的链接地址
const activePath = ref('')
activePath.value = sessionStorage.getItem('activePath')

async function useGetMenuList() {
  const { data: res } = await getMenuList()
  if (res.meta.status !== 200) {
    // eslint-disable-next-line no-undef
    return ElMessage.error(res.meta.msg)
  }
  menuList.value = res.data
}
useGetMenuList()

// 是否折叠
const isCollapse = ref(false)

// 是否折叠
function toggleCollapse() {
  isCollapse.value = !isCollapse.value
}
// 保存链接的激活状态
function saveNavState(path) {
  sessionStorage.setItem('activePath', path)
  activePath.value = path
}
// /退出按钮
function logout() {
  userStore.removeToken()
  router.push('/login')
}
</script>

<template>
  <el-container class="home-container">
    <el-header>
      <div>
        <img src="../assets/R.png" alt="">
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">
        退出
      </el-button>
    </el-header>
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <!-- 点击之后折叠区域 -->
        <div class="toggle_button" @click="toggleCollapse">
          |||
        </div>
        <el-menu
          :default-active="activePath"
          router
          background-color="#333744"
          text-color="#fff"
          :collapse="isCollapse"
          :unique-opened="true"
          :collapse-transition="false"
          active-text-color="#409eff"
        >
          <el-sub-menu
            v-for="item in menuList"
            :key="item.id"
            :index="`${item.id}`"
          >
            <template #title>
              <i :class="iconsObj[item.id]" />
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单区域 -->
            <el-menu-item
              v-for="subItem in item.children"
              :key="subItem.id"
              :index="`/${subItem.path}`"
              @click="saveNavState(`/${subItem.path}`)"
            >
              <template #title>
                <el-icon>
                  <Menu />
                </el-icon>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <!-- main -->
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </el-container>
</template>

<style lang="scss" scoped>
.el-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-left: 0;
  font-size: 20px;
  color: #fff;
  background-color: #373d41;

  div {
    display: flex;
    align-items: center;

    img {
      width: 50px;
      height: 50px;
    }

    span {
      margin: 0 15px;
    }
  }
}

.el-aside {
  background-color: #333744;

  .el-menu {
    border: none;
  }
}

.toggle_button {
  font-size: 14px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
  background-color: #4a5064;
}

.home-container {
  height: 100%;
}
</style>
